package com.yinhai.asynctask;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yinhai.model.IMGroup;
import com.yinhai.service.IMGroupServiceImpl;

import android.content.Context;

public class GetUserGroupsTask  extends AsyncTaskForHttp{
	String retStr = null;
	private Context context;
	private IMGroupServiceImpl imGroupService;
	public GetUserGroupsTask(Context context) {
		super(context,false);
		this.context = context;
		imGroupService = new IMGroupServiceImpl(context);
	}
	
	public GetUserGroupsTask(Context context, boolean autoForword) {
		super(context, autoForword);
	}

	public GetUserGroupsTask(Context context, int timeoutRequestCount,
			boolean autoForword) {
		super(context, timeoutRequestCount, autoForword);
	}
	@Override
	protected void parse(Map<String, Object> result) {
		this.retStr = (String)result.get(AsyncTaskContext.RESPONSE_CONTENT);
		JSONObject jo;
		try {
			jo = new JSONObject(retStr);
			JSONObject jo1 = null;
			if("0".equals(jo.getString("status"))) {
				JSONArray ja = jo.getJSONArray("dataList");
				IMGroup group = null;
				for(int i = 0; i < ja.length(); i++) {
					jo1 = ja.getJSONObject(i);
					//先删除历史数据
					imGroupService.delete(jo1.getString("group_id"));
					Map<String,Object> map=new HashMap<String, Object>();
					group = new IMGroup();
					group.setGroupId(jo1.getString("group_id"));
					group.setGroupName(jo1.getString("group_name"));
					group.setUserPhone(jo1.getString("user_phone"));
					imGroupService.insert(group);
				}
			} 
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
